﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TWS.Controller.Paging;
using HtmlAgilityPack;
using System.IO;

public partial class DanhSachTinTuc : BaseAdminPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            LoadTinTuc();
            MasterPage master = (MasterPage)this.Master;
            LoadDoiTac();
            LoadHotrotructuyen();
        }
    }

    private void LoadTinTuc()
    {
        string tmpPage = Request.QueryString["pg"];
        int currentPage = 1;
        if (tmpPage != null && tmpPage != "")
        {
            currentPage = Convert.ToInt32(tmpPage);
        }
        TinTucDataSet spDs = new TinTucHelper().LoadTinTuc(currentPage, PagingSize, "NgayDang", "DESC");

        repLoaiSanPham.DataSource = spDs.TinTuc;
        repLoaiSanPham.DataBind();

        TotalRecord = new TinTucHelper().LoadTinTuc().TinTuc.Rows.Count;

        ResultCount.Text = TotalRecord.ToString();

        Paging page = getPagingObject(TotalRecord, currentPage, PagingSize);

        PageString = page.BuildPaging();
    }


    public string GetNgayTao(object ngaytao)
    {
        if (ngaytao != DBNull.Value)
        {
            return CommonHelper.ConvertDate(ngaytao, Constants.UserUI);

        }
        return "";
    }

    public string GrabImage(string htmlContent)
    {
        String firstImage;

        HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
        htmlDoc.LoadHtml(htmlContent);
        HtmlAgilityPack.HtmlNode imageNode = htmlDoc.DocumentNode.SelectSingleNode("//img");
        if (imageNode != null)
        {
            HtmlAttribute src = imageNode.Attributes["src"];
            firstImage = src.Value;

        }
        else
            firstImage = " ";
        return firstImage;
    }

    public string GrabNoiDung(string htmlContent)
    {

        return CommonHelper.RutGonString( ConvertHtml(htmlContent),100);
    }

    private void ConvertContentTo(HtmlNode node, TextWriter outText)
    {
        foreach (HtmlNode subnode in node.ChildNodes)
        {
            ConvertTo(subnode, outText);
        }
    }

    public string ConvertHtml(string html)
    {
        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(html);

        StringWriter sw = new StringWriter();
        ConvertTo(doc.DocumentNode, sw);
        sw.Flush();
        return sw.ToString();
    }

    public void ConvertTo(HtmlNode node, TextWriter outText)
    {
        string html;
        switch (node.NodeType)
        {
            case HtmlNodeType.Comment:
                // don't output comments
                break;

            case HtmlNodeType.Document:
                ConvertContentTo(node, outText);
                break;

            case HtmlNodeType.Text:
                // script and style must not be output
                string parentName = node.ParentNode.Name;
                if ((parentName == "script") || (parentName == "style"))
                    break;

                // get text
                html = ((HtmlTextNode)node).Text;

                // is it in fact a special closing node output as text?
                if (HtmlNode.IsOverlappedClosingElement(html))
                    break;

                // check the text is meaningful and not a bunch of whitespaces
                if (html.Trim().Length > 0)
                {
                    outText.Write(HtmlEntity.DeEntitize(html));
                }
                break;

            case HtmlNodeType.Element:
                switch (node.Name)
                {
                    case "p":
                        // treat paragraphs as crlf
                        outText.Write("\r\n");
                        break;
                }

                if (node.HasChildNodes)
                {
                    ConvertContentTo(node, outText);
                }
                break;
        }
    }

    private void LoadDoiTac()
    {
        DoiTacDataSet bnDS = new DoiTacHelper().LoadDoiTac();
        repDoiTac.DataSource = bnDS;
        repDoiTac.DataBind();
    }

    private void LoadHotrotructuyen()
    {
        dllOnline.DataSource = new HoTroTrucTuyenHelper().LoadHoTroTrucTuyen();
        dllOnline.DataBind();
    }
}